package com.digital.smartgrow.controller.businesscenter;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.common.util.WebTool;
import com.digital.smartgrow.dto.businesscenter.DynamicFormTemplatePageDTO;
import com.digital.smartgrow.entity.businesscenter.DynamicFormTemplate;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.service.businesscenter.DynamicFormTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 动态表单模板 前端控制器
 * </p>
 *
 * @author xiewb
 * @since 2022-04-23
 */
@Api(tags = "动态表单模板")
@RestController
@RequestMapping("${context.prefix}/dynamicFormTemplate")
public class DynamicFormTemplateController {

    @Autowired
    private DynamicFormTemplateService dynamicFormTemplateService;

    @ApiOperation(value = "列表查询")
    @PostMapping("/getList")
    public Object getList(@RequestBody DynamicFormTemplatePageDTO dynamicFormTemplatePageDTO){
        return dynamicFormTemplateService.getList(dynamicFormTemplatePageDTO);
    }

    @ApiOperation(value = "动态表单模板-详情")
    @GetMapping("/getInfo")
    public Object getInfo(Integer id){
        if (id == null) return "id不能为空";
        return dynamicFormTemplateService.getInfo(id);
    }

    @ApiOperation(value = "动态表单模板-删除")
    @GetMapping("/removeById")
    public Object removeById(Integer id){
        if (id == null) return Result.fail("id不能为空");
        return dynamicFormTemplateService.removeById(id);
    }

    @ApiOperation(value = "动态表单模板-新增")
    @PostMapping("/add")
    public Object add(@Valid @RequestBody DynamicFormTemplate dynamicFormTemplate){
        Teacher teacher = WebTool.getCurrentUser();
        dynamicFormTemplate.setCreateId(teacher.getId());
        return dynamicFormTemplateService.save(dynamicFormTemplate);
    }

    @ApiOperation(value = "动态表单模板-编辑")
    @PostMapping("/edit")
    public Object edit(@Valid @RequestBody DynamicFormTemplate dynamicFormTemplate){
        if (dynamicFormTemplate == null || dynamicFormTemplate.getId() == null) return Result.fail("id不能为空");
        return dynamicFormTemplateService.updateById(dynamicFormTemplate);
    }
}
